<?php
namespace app\shop\library\coupon;

use cubox\Cubox;
use app\shop\library\ApiController;
use app\shop\library\coupon\Coupon;
use app\shop\library\Member;
use app\shop\library\order\Order;

class MemberCoupon{

    public static function count(){
         $list = self::list();
         return count($list);
    }
    public static function list(){
         
        $shop =  Cubox::app('shop');
        $key = Member::cacheName('coupon');
        $cache = $shop->cache();
        $relist  = [];
        if ($cache->exists($key)){
            $hashList = $cache->hGetAll($key);

            foreach($hashList as $key=>$item){
                $relist[] = json_decode($item,true);
            }
        }else{
            $MemberCoupon = $shop->model('MemberCoupon');
            $list = $MemberCoupon
                    ->where('member_id',Member::id())
                    ->where('status','new')
                    ->where('coupon_starttime','<',date('Y-m-d H:i:s'))
                    ->where('coupon_endtime','>',date('Y-m-d H:i:s'))
                    ->getData();

            foreach($list as $data){

                $coupon['coupon_id'] = $data['coupon_id'];
                $coupon['starttime'] = $data['coupon_starttime'];
                $coupon['endtime']   = $data['coupon_endtime'];
                $coupon['price']     = $data['coupon_price'];
                $coupon['type']      = $data['coupon_type'];
                $coupon['condition_price'] = $data['coupon_condition_price'];
                $coupon['apply_type'] = $data['coupon_apply_type'];
                $coupon['apply_goods_list'] = [];
                $coupon['apply_cate_list']  = [];
                $coupon['desc'] =  $data['coupon_desc'];

                if ($data['coupon_apply_type'] !='all'){
                    if ($data['coupon_apply_goods_list']!='') $coupon['apply_goods_list'] = explode(',',$data['coupon_apply_goods_list']);
                   
                    if ($data['coupon_apply_cate_list']!=''){
                        $cate = $shop->model('Cate');

                        $cateList = [];

                        foreach (explode(',',$data['coupon_apply_cate_list']) as $cateId){
                            $cateList = array_unique(array_merge($cateList,$cate->getKeyCollect($cateId,true,true)));
                        }

                        $coupon['apply_cate_list'] = $cateList;
                    }
                }

                //删除不用的键
                unset($data['coupon_starttime']);
                unset($data['coupon_endtime']);
                unset($data['coupon_price']);
                unset($data['coupon_condition_price']);
                unset($data['coupon_apply_type']);
                unset($data['coupon_type']);
                unset($data['coupon_desc']);
                unset($data['coupon_apply_goods_list']);
                unset($data['coupon_apply_cate_list']);
                
                $data['coupon'] = $coupon;

                $relist[] = $data;
                $cache->hSet($key,$data['member_coupon_id'],json_encode($data));
            }
        }

        return $relist;
    }
    public static function get($member_coupon_id){
        $app =  Cubox::app('shop');
        $key = Member::cacheName('coupon');
        
        $cache = $app->cache();

        if (!$cache->exists($key)){
            self::list();
        }

        $couponItem = $cache->hGet($key,$member_coupon_id);

        if ($couponItem){
            return json_decode($couponItem,true);
        }else{
            return null;
        }
    }


    public static function use(string | array $member_coupon_id,$member_id = ''){
        $app =  Cubox::app('shop');
        $key = Member::cacheName('coupon',$member_id);

        $cache = $app->cache();

        if (is_array($member_coupon_id)){
            foreach($member_coupon_id as $id){
                $cache->hDel($key,$id);
            }
        }else{
            $cache->hDel($key,$member_coupon_id);
        }
        //删除缓存的值
        
        return true;
    }

    public static function clearCache($member_id=''){
        $app =  Cubox::app('shop');
        $key = Member::cacheName('coupon',$member_id);
        $cache = $app->cache();
        //删除缓存的值
        $cache->del($key);
    }

    public static function  exist(array $ids):array{
        $app =  Cubox::app('shop');
        $cache  = $app->cache();
        $existListKey = Member::cacheName('coupon_exist');
        $existList = json_decode($cache->get($existListKey)??'{}',true);
        
        $result = [];
        foreach($ids as $index=>$id){
            if (isset($existList[$id])){
                $result[$id] = $existList[$id];
                unset($ids[$index]);
            }else{
                $result[$id] = 0;
            }
        }

        if (count($ids)>0){
            $list = $app->model('MemberCoupon')->where('member_id',Member::id())->where('coupon_id','in',$ids)->getData();

            foreach ($list as $coupon){
                if (isset($existList[$coupon['coupon_id']])){
                    $existList[$coupon['coupon_id']] ++;
                }else{
                    $existList[$coupon['coupon_id']] = 1;
                }
                
                $result[$coupon['coupon_id']] = $existList[$coupon['coupon_id']];
            }

            $cache->set($existListKey,json_encode($existList));
        }

        return $result;
    }

    public static function updateExist($id,$num){
        $app =  Cubox::app('shop');
        $cache  = $app->cache();
        $existListKey = Member::cacheName('coupon_exist');
        $existList = json_decode($cache->get($existListKey)??'{}',true);
        $existList[$id] = $num;
        $cache->set($existListKey,json_encode($existList));
        return true;
    }

    
}